import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JFrame {
    private static JFrame JFrame = new JFrame();
    private javax.swing.JFrame jFrame = new javax.swing.JFrame();
    private Container container = jFrame.getContentPane();
    private JButton jButton = new JButton("读取文件");
    private JComboBox jComboBox = new JComboBox();
    private JButton PlainButton = new JButton("Display team (Plain Text)");
    private JButton HTMLButton = new JButton("Display team (HTML)");
    private JButton XMLButton = new JButton("Display team (XML)");
    private JTextArea jTextArea = new JTextArea();

    private List<Team> teamList = new ArrayList<>();
    private Team nowTeam = null;
    private MouseListener loadData = new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
            BufferedReader teamDates = null;
            try {
                teamDates = new BufferedReader(new FileReader("team.dat"));
                String tmp = null;
                while (true) {
                    if (!((tmp = teamDates.readLine()) != null)) break;

                    //可能读到空行
                    if(tmp.length() == 0){
                        continue;
                    }

                    String[] teamMember = tmp.split("_");

                    //记录属性总个数
                    int studentNum = 0;
                    int teacherNum = 0;

                    int studentStartIndex = 0;
                    int teacherStartIndex = 0;

                    //将男，女替换成boolean类型，防止类型出错
                    for (int i = 9; i < teamMember.length; i++) {
                        if (teamMember[i].equals("女")) {
                            teamMember[i] = "true";
                        } else if (teamMember[i].equals("男")) {
                            teamMember[i] = "false";
                        }
                    }

                    for (int i = 13; i < teamMember.length; i++) {
                        if (teamMember[i].equals("Student")) {
                            int j = i+1;
                            studentStartIndex = j;//记录下学生开始的位置
                            while(j < teamMember.length){
                                if(teamMember[j].equals("Teacher")){
                                    break;
                                }
                                studentNum++;
                                j++;
                            }
                        }else if(teamMember[i].equals("Teacher")){
                            int j = i+1;
                            teacherStartIndex = j;//记录下老师开始的位置
                            while(j < teamMember.length){
                                teacherNum++;
                                j++;
                            }
                        }
                    }

                    //学生属性个数不对，抛出异常
                    if(studentNum % 8 != 0){
                        System.out.println("studentNum = " + studentNum);
                        throw new RuntimeException("学生属性个数异常");
                    }

                    //老师属性个数不对，抛出异常
                    if(teacherNum % 6 != 0){
                        throw new RuntimeException("老师属性个数异常");
                    }

                    int studentCount = studentNum /= 8;
                    int teacherCount = teacherNum /= 6;

                    Student creator = new Student(teamMember[5], teamMember[6], teamMember[7], teamMember[8], teamMember[9], Boolean.parseBoolean(teamMember[10]), teamMember[11], teamMember[12]);
                    Team team = new Team(teamMember[1], teamMember[2], teamMember[3], creator);
                    teamList.add(team);

                    //添加学生
                    if(studentCount != 0){
                        for (int i = 0; i < studentCount; i++) {
                            team.addStudent(new Student(teamMember[studentStartIndex + i*8], teamMember[studentStartIndex+1 + i*8], teamMember[studentStartIndex+2 + i*8], teamMember[studentStartIndex+3 + i*8], teamMember[studentStartIndex+4 + i*8], Boolean.parseBoolean(teamMember[studentStartIndex+5 + i*8]), teamMember[studentStartIndex+6 + i*8], teamMember[studentStartIndex+7 + i*8]));
                        }
                    }

                    //添加老师
                    if(teacherCount != 0){
                        for (int i = 0; i < teacherCount; i++) {
                            team.addTeacher(new Teacher(teamMember[teacherStartIndex + i*8], teamMember[teacherStartIndex+1 + i*8], teamMember[teacherStartIndex+2 + i*8], teamMember[teacherStartIndex+3 + i*8], teamMember[teacherStartIndex+4 + i*8], teamMember[teacherStartIndex+5 + i*8]));
                        }
                    }
                }

                int n = teamList.size();
                String[] teamNameList = new String[n];
                for (int i = 0; i < teamList.size(); i++) {
                    teamNameList[i] = teamList.get(i).getTeamId();
                    jComboBox.addItem(teamNameList[i]);
                }
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    };

    private MouseListener plainButtonListener = new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println(PlainTextTeamFormatter.getSingletonInstance().formatTeam(nowTeam));
            jTextArea.setText(PlainTextTeamFormatter.getSingletonInstance().formatTeam(nowTeam));
        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    };
    private MouseListener htmlButtonListener = new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println(HTMLTeamFormatter.getSingletonInstance().formatTeam(nowTeam));
            jTextArea.setText(HTMLTeamFormatter.getSingletonInstance().formatTeam(nowTeam));
        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    };
    private MouseListener xmlButtonListener = new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println(XMLTeamFormatter.getSingletonInstance().formatTeam(nowTeam));
            jTextArea.setText(XMLTeamFormatter.getSingletonInstance().formatTeam(nowTeam));
        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    };

    public void init() {
        jFrame.setSize(1130, 800);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(3);
        jFrame.setTitle("团队信息查看系统");

        container.setLayout(new FlowLayout());

        container.add(jButton);
        ButtonListener buttonListener = new ButtonListener();
        jButton.addMouseListener(loadData);


        jComboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {

                if (e.getStateChange() == ItemEvent.SELECTED) {
                    System.out.println(e.getItem());

                    for (Team k : teamList) {
                        if (k.getTeamId() == e.getItem()) {
                            nowTeam = k;
                        }
                    }
                }
            }
        });
        container.add(jComboBox);

        container.add(PlainButton);

        PlainButton.addMouseListener(plainButtonListener);

        container.add(HTMLButton);

        HTMLButton.addMouseListener(htmlButtonListener);

        container.add(XMLButton);

        XMLButton.addMouseListener(xmlButtonListener);

        container.add(jTextArea);

        jFrame.setVisible(true);
    }
}









